package com.sphd.Restaurant.fragments.msgsFragments;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sphd.Restaurant.R;
import com.sphd.Restaurant.base.LazyLoadFragment;
import com.sphd.Restaurant.infos.MsgsInfo;
import com.sphd.Restaurant.model.Model;
import com.sphd.Restaurant.presenter.MsgP;
import com.sphd.Restaurant.utils.Constants;
import com.sphd.Restaurant.views.MsgView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Hu on 2015/12/11.
 * 系统消息 界面
 */
public class SystemMsgFragment extends LazyLoadFragment implements MsgView{

    MsgP msgP;
    List<MsgsInfo> msgsInfos;
    BaseQuickAdapter<MsgsInfo> baseQuickAdapter;
    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_srf)
    SwipeRefreshLayout mySrf;

    @Override
    public int getLayout() {
        return R.layout.fragment_dis_msg;
    }

    @Override
    public void initViews(View view) {
        ButterKnife.bind(this, view);
        msgP = new MsgP(this);
        msgsInfos = new ArrayList<>();
        setRefresh(mySrf);
        setRecy(myRecy);
        baseQuickAdapter = new BaseQuickAdapter<MsgsInfo>(R.layout.item_fragment_msgs, msgsInfos) {
            @Override
            protected void convert(BaseViewHolder helper, MsgsInfo item) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
                helper.setText(R.id.tv_time, format.format(item.getRegtime()))
                        .setText(R.id.tv_name, item.getCgname())
                        .setText(R.id.tv_msg, item.getMessage());

            }
        };
        myRecy.setAdapter(baseQuickAdapter);
        mySrf.setOnRefreshListener(() -> mySrf.setRefreshing(false));
    }

    @Override
    public void loadData() {
        msgP.addData(Model.HTTPURL + Model.MESSAGE + "&phone=" + getString(Constants.USERPHONE) + "&type=0");
    }

    @Override
    public void sendRes(List<MsgsInfo> msgsInfoList) {
        baseQuickAdapter.setNewData(msgsInfoList);
    }

    @Override
    public void showdialog() {
        showDialog();
    }

    @Override
    public void dissmissdialog() {
dissMissDialog();
    }
}
